package org.example.demo.config;

import com.kfyang.manualgear.annotation.global.CopyAnnotation;
import com.kfyang.manualgear.annotation.global.DaoConfig;
import com.kfyang.manualgear.annotation.global.ResultHandler;
import com.kfyang.manualgear.annotation.global.TypeHandler;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Optional;
import java.util.function.Function;

@DaoConfig
@CopyAnnotation({Transactional.class})
public interface AnnoBatisConfig {
    @TypeHandler
    static Date dateHandler(ResultSet rs, String col) throws SQLException {
        return rs.getDate(col);
    }

    @ResultHandler
    static <T> Optional<T> optionalResult(Function<String, T> oneFunction,
                                        StringBuilder sql) {
        return Optional.ofNullable(oneFunction.apply(sql.toString()));
    }
}

